<template>
    <n-form
        style="margin-top: 12px"
        ref="formRef"
        :model="formData"
        :rules="rules"
        label-placement="left"
        label-width="100"
        require-mark-placement="right-hanging"
        size="medium"
    >
        <n-form-item label="知识库" path="knowledgeBaseId">
            <jb-select :disabled="props.id || props.knowledgeBaseId" v-model:value="formData.knowledgeBaseId"
                       url="/aiKnowledgeBase/listAllEnabled" labelField="name" valueField="id" :filterable="true"></jb-select>
        </n-form-item>

        <n-form-item label="训练类型" path="docType">
            <jb-radio-group v-model:value="formData.docType" url="/aiFile/getTrainList">

            </jb-radio-group>
        </n-form-item>

        <n-form-item label="问题/内容" path="content">
            <n-input
                v-model:value="formData.content"
                type="textarea"
                placeholder="请输入问题或内容"
                :autosize="{
                    minRows: 3,
                    maxRows: 15
                }"
            />
        </n-form-item>

        <n-form-item
            v-if="formData.docType == 'QA'"
            label="答案"
            path="answer"
        >
            <n-input
                v-model:value="formData.answer"
                type="textarea"
                placeholder="请输入答案"
                :autosize="{
                    minRows: 3,
                    maxRows: 15
                }"
            />
        </n-form-item>


    </n-form>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import {useMessage} from 'naive-ui'
import {JBoltApi} from '@/service/request'
import {useResetableData} from "@/hooks/common/use-resetable-data.js";

const props = defineProps({
    id: String,
    knowledgeBaseId: String
})

const message = useMessage()
const formRef = ref(null)

// 表单数据
const formData = useResetableData({
    id: '',
    knowledgeBaseId: props.knowledgeBaseId,
    content: '',
    answer: '',
	dataType:"customText",
    docType: 'normal',
    linkId: null,
    fileId: null,
    docStatus: 0,
    trainTime: "",
    vectorTime: "",
    createTime: ""
})

// 表单验证规则
const rules = {
    knowledgeBaseId: {
        required: true,
        message: '请选择知识库',
        trigger: 'change'
    },
    // dataType: {
    //     required: true,
    //     message: '请选择训练类型',
    //     trigger: 'change'
    // },
    content: {
        required: true,
        message: '请输入问题或内容',
        trigger: 'blur'
    },
    answer: {
        required: true,
        message: '请输入答案',
        trigger: 'blur'
    }
}

// 提交方法
async function submit(fun) {
    await formRef.value?.validate()


    let url = props.id ? '/aiDoc/update' : '/aiDoc/add'

    if (props.id) {

        await JBoltApi.post(url, formData)
    } else {
        await JBoltApi.tryPost(url, formData)
    }


    if (fun) {
        fun()
    }

    return true
}

// 加载详情
const loadDetail = async () => {
    if (!props.id) return

        const {result} = await JBoltApi.get(`/aiDoc/getById/${props.id}`)
        if(result){
            console.debug('详情:', result.data)
            formData._reset(result.data)
        }

}

onMounted(() => {
    if (props.id) {
        loadDetail()
    }
})

defineExpose({
    submit
})
</script>